package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.PestPageRequest;
import com.example.springboot.controller.request.savePestRequest;
import com.example.springboot.entity.Pest;
import com.example.springboot.exception.FileUploadException;
import com.example.springboot.service.IPestService;
import com.example.springboot.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/pest")
public class PestController {

    @Autowired
    private IPestService pestService;
    @Autowired
    private FileUtil fileUtil;


    // 接口一：保存 pest 数据，返回生成的 id
    @PostMapping("/save")
    public Result save(@RequestBody savePestRequest obj) {
        Integer pestId = pestService.save(obj); // 调用已有的 save 方法，返回生成的 pestId
        return Result.success(pestId);
    }


    // 接口二：上传图片并关联 pest
    @PostMapping("/upload/{pestId}/images")
    public Result upload(@PathVariable Integer pestId,
                         @RequestPart("files") List<MultipartFile> files) {
        List<String> urls = new ArrayList<>();
        for (MultipartFile file : files) {
            try {
               String url = fileUtil.upload(file);
               urls.add(url);
            } catch (FileUploadException e) {
               log.error("上传文件 [{}] 失败", file.getOriginalFilename(), e);
           }
        }

        pestService.addImagesToPest(pestId, urls);
        return Result.success(pestId);
    }


    // 添加接口三：更新 pest
    @PutMapping("/update")
    public Result update(@RequestBody savePestRequest obj) {
        pestService.update(obj);
        return Result.success(obj);
    }


    /**
     * 删除 pest
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        pestService.deleteById(id);
        return Result.success();
    }

    /**
      * 删除 pest 图片
      * @param id
      * @return
      */
    @DeleteMapping("/image/delete/{id}")
    public Result deleteImage(@PathVariable Integer id) {
        pestService.deleteImage(id);
        return Result.success(id);
    }


    /**
     * 根据 id 查询 pest
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Pest obj = pestService.getById(id);
        return Result.success(obj);
    }

    /**
     * 查询 pest 列表
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        List<Pest> list = pestService.list();
        return Result.success(list);
    }

    /**
     * 分页查询
     * @param pageRequest
     * @return
     */
    @GetMapping("/page")
    public Result page(PestPageRequest pageRequest) {
        return Result.success(pestService.page(pageRequest));
    }

}
